package com.ydc.ln.tutorial.gui.test.event;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * 
 * @author ydc
 * @date 上午11:07:40 2013-5-18
 */
public class WindowEventFrame extends WindowAdapter implements ActionListener{
    JButton b1 = null;
    JButton b2 = null;
    
    String but1Cmd = "声音";
    String but2Cmd = "新窗口";
    
    public static void main(String[] args) {
        new WindowEventFrame();
    }
    
    public WindowEventFrame() {
        JFrame f = new JFrame("WindowEvent");
        Container contentPane = f.getContentPane();
        contentPane.setLayout(new GridLayout(1, 2));
        
        b1 = new JButton(but1Cmd);
        b2 = new JButton(but2Cmd);
        
        b1.addActionListener(this);
        b2.addActionListener(this);
        
        contentPane.add(b1);
        contentPane.add(b2);
        
        f.pack();
        f.setVisible(true);
        
        f.addWindowListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if((e.getActionCommand()).equals(but1Cmd)) {
            System.out.println("-----------");
            // Toolkit.getDefaultToolkit().beep();
        }
        if((e.getActionCommand()).equals(but2Cmd)) {
            JFrame newF = new JFrame(but2Cmd);
            newF.setSize(200, 200);
            newF.setVisible(true);
        }
    }
    
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}
